package com.ybxx.fees.service.impl;

import java.util.List;

import com.ybxx.base.model.Residents;
import com.ybxx.base.model.Room;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.dao.GenericDao;
import com.ybxx.core.service.impl.BaseServiceImpl;
import com.ybxx.core.web.paging.PagingBean;
import com.ybxx.fees.dao.WatermeterRecordsDao;
import com.ybxx.fees.model.AnmeterRecords;
import com.ybxx.fees.model.WatermeterRecords;
import com.ybxx.fees.service.WatermeterRecordsService;

public class WatermeterRecordsServiceImpl extends
		BaseServiceImpl<WatermeterRecords> implements WatermeterRecordsService {
	private WatermeterRecordsDao watermeterRecordsDao;
	public WatermeterRecordsServiceImpl(WatermeterRecordsDao dao) {
		super(dao);
		this.watermeterRecordsDao=dao;
		// TODO Auto-generated constructor stub
	}
	public WatermeterRecords generator(Room room,Residents residents,String ymonth){
		WatermeterRecords watermeterRecords=null;
		String sql="select concat(rmin,'~',rmax) as target,case when rmin<actual and rmax<=actual then rmax " +
				"when rmin<actual and rmax>=actual then (actual-rmin) end meter,price," +
				"case when rmin<actual and rmax<=actual then rmax*price " +
				"when rmin<actual and rmax>=actual then (actual-rmin)*price end fees," +
				"thereadings,currentreading,walkquantity,amount,actual " +
				"from tbl_base_res_hydropower," +
				"(select * from tbl_dashboard_watermeter_read where rid='"+room.getRid()+"' and ymonth='"+ymonth+"') m " +
				"where rcode='"+residents.getRcode()+"' and tid='001' order by rmin ";
		watermeterRecords=watermeterRecordsDao.generator(sql);
		if(watermeterRecords==null){
			sql="select concat(rmin,'~',rmax) as target,case when rmin<actual and rmax<=actual then rmax " +
				"when rmin<actual and rmax>=actual then (actual-rmin) end meter,price," +
				"case when rmin<actual and rmax<=actual then rmax*price " +
				"when rmin<actual and rmax>=actual then (actual-rmin)*price end fees," +
				"thereadings,currentreading,walkquantity,amount,actual " +
				"from tbl_base_hydropower h,tbl_base_hydropower_item hi," +
				"(select * from tbl_dashboard_watermeter_read where rid='"+room.getRid()+"' and ymonth='"+ymonth+"') m " +
				"where h.hcode=hi.hcode and cid='"+room.getUnit().getBan().getCommunity().getCid()+"' and tid='001' order by rmin";
			watermeterRecords=watermeterRecordsDao.generator(sql);
		}
		return watermeterRecords;
	}
}
